package cn.botter.auth.domain.service.impl;

import cn.botter.auth.common.enums.IsDeletedEnum;
import cn.botter.auth.common.enums.IsShowEnum;
import cn.botter.auth.common.enums.StatusChangeEnum;
import cn.botter.auth.domain.convert.AuthPermissionConvert;
import cn.botter.auth.domain.entity.AuthPermissionBO;
import cn.botter.auth.domain.redis.RedisUtil;
import cn.botter.auth.domain.service.AuthPermissionDomainService;
import cn.botter.auth.infra.basic.entity.AuthPermission;
import cn.botter.auth.infra.basic.service.AuthPermissionService;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: bo-club
 * @BelongsPackage: cn.botter.auth.domain.service.impl
 * @Author: 爱写Bug的Botter
 * @CreateTime: 2025-03-17:08:28
 * @Description: 描述
 * @Version: 1.0
 */
@Service
@Slf4j
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {
    @Resource
    private RedisUtil redisUtil;
    private String authPermissionPrefix = "auth.permission";
    @Resource
    private AuthPermissionService authPermissionService;
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConvert.INSTANCE.convertBoToPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        authPermission.setStatus(StatusChangeEnum.OPEN.getCode());
        authPermission.setShow(IsShowEnum.ENABLE.getCode());
        Integer result = authPermissionService.insert(authPermission);

        return result > 0 ;
    }

    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        AuthPermission authPermission = AuthPermissionConvert.INSTANCE.convertBoToPermission(authPermissionBO);

        return authPermissionService.update(authPermission) > 0;
    }

    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {

        AuthPermission authPermission = AuthPermissionConvert.INSTANCE.convertBoToPermission(authPermissionBO);
        authPermission.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        return authPermissionService.update(authPermission) > 0;

    }

    @Override
    public List<String> gerPermission(String username) {
        String buildKey = redisUtil.buildKey(authPermissionPrefix, username);
        String value = redisUtil.get(buildKey);
        if (StringUtils.isBlank(value)){
            return Collections.emptyList();
        }
        //对redis拿取的数据进行序列化
        List<AuthPermission> permissionList = new Gson().fromJson(value,
                new TypeToken<List<AuthPermission>>() {
                }.getType());
        List<String> authList = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }
}
